package com.baiqian.doupo.block.lian_yao_ding;

import com.baiqian.doupo.item.DanYao;
import com.baiqian.doupo.tileentity.HuoDingTileEntity;
import net.minecraft.block.Block;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.block.material.Material;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.item.ExperienceOrbEntity;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.item.ItemStack;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.DirectionProperty;
import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
import java.util.function.ToIntFunction;

public class HuoDing extends LianYaoDing {
    public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
    public static final BooleanProperty LIT = BlockStateProperties.LIT;

//    public static final BooleanProperty ENABLED = BlockStateProperties.ENABLED;
    /**
     * 炼丹炉常温
     * */
    public static final double BASE_HOT = 21;
    /**
     * 基本炼化温度，超过这个温度则算作开始一次炼丹
     * */
    public static final double MIN_HOT = 100;
    /**
     * 最高承受温度，超过这个温度则直接炸炉
     * */
    public static final double MAX_HOT = 600;
    /**
     * 同时炼化药材量
     * */
    public static final int MAX_NUM = 3;

    public HuoDing() {
        super(Properties.create(Material.ROCK).hardnessAndResistance(0).setLightLevel(getLightValueLit(13)));
        this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH).with(LIT, Boolean.valueOf(false)));
    }

    private static VoxelShape shape;

    static {
//        VoxelShape base = Block.makeCuboidShape(0, 0, 0, 16, 1, 16);
//        VoxelShape column1 = Block.makeCuboidShape(0, 1, 0, 1, 15, 1);
//        VoxelShape column2 = Block.makeCuboidShape(15, 1, 0, 16, 15, 1);
//        VoxelShape column3 = Block.makeCuboidShape(0, 1, 15, 1, 15, 16);
//        VoxelShape column4 = Block.makeCuboidShape(15, 1, 15, 16, 15, 16);
//        VoxelShape top = Block.makeCuboidShape(0, 15, 0, 16, 16, 16);
//        shape = VoxelShapes.or(base, column1, column2, column3, column4, top);
        VoxelShape all = Block.makeCuboidShape(1, 1, 1, 15, 15, 15);
        shape = VoxelShapes.or(all);
    }

    @Override
    public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
        return shape;
    }

    //似乎创建方块
    @Nullable
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing());
    }

    @Override
    protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(FACING, LIT);
    }

    @Override
    public BlockState rotate(BlockState state, Rotation rot) {
        return state.with(FACING, rot.rotate(state.get(FACING)));
    }

    @Override
    public BlockState mirror(BlockState state, Mirror mirrorIn) {
        return mirrorIn == Mirror.NONE ? state : state.rotate(mirrorIn.toRotation(state.get(FACING)));
    }

    @Override//破坏方块
    public void onBlockHarvested(World worldIn, BlockPos pos, BlockState state, PlayerEntity player) {
        super.onBlockHarvested(worldIn, pos, state, player);
        if (!worldIn.isRemote) {
        }
    }


    @Override//这组函数让你的方块可以绑定一个方块实体，hasTileEntity返回值设置为true，说明我们这个方块拥有一个方块实体
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    @Nullable
    @Override//这个函数决定了我们方块的方块实体具体是哪一个，在我们的例子里是ObsidianCounterTileEntity
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new HuoDingTileEntity();
    }

    @Override//方块实体的逻辑
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
        if (!worldIn.isRemote && handIn == Hand.MAIN_HAND) {
            //获取到我们方块所对应的TileEntity
            HuoDingTileEntity huoDingTileEntity = (HuoDingTileEntity) worldIn.getTileEntity(pos);
            if(huoDingTileEntity.isMaking()){
                if(huoDingTileEntity.getMakingItem().size()==0){
                    //试图返回丹药
                    ItemStack danYaoStack = DanYao.getDanYao(huoDingTileEntity.getFinishItem(),huoDingTileEntity.getQuality());
                    if(danYaoStack!=null){
                        huoDingTileEntity.stopMaking();
                        huoDingTileEntity.throwItemBack();
                        worldIn.addEntity(new ItemEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), danYaoStack));
                        worldIn.addEntity(new ExperienceOrbEntity(worldIn, pos.getX(), pos.getY(), pos.getZ(), 1));
                    }else{
                        //返还
                        huoDingTileEntity.throwItemBack(player);
                        huoDingTileEntity.stopMaking();
                    }
                }else{
                    //返还
                    huoDingTileEntity.throwItemBack(player);
                    huoDingTileEntity.stopMaking();
                }
            }else{
                //返还
                huoDingTileEntity.throwItemBack(player);
            }
        }
        return ActionResultType.SUCCESS;
    }

    @Override
    public BlockRenderType getRenderType(BlockState state) {
        return super.getRenderType(state);
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void addInformation(ItemStack stack, @Nullable IBlockReader worldIn, List<ITextComponent> tooltip, ITooltipFlag flagIn) {
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent(this.getTranslationKey() + ".1").getString() + MIN_HOT));
        tooltip.add(ITextComponent.getTextComponentOrEmpty(new TranslationTextComponent(this.getTranslationKey() + ".2").getString() + MAX_HOT));
    }

    @OnlyIn(Dist.CLIENT)
    public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
        /**散发火焰特效*/
        HuoDingTileEntity huoDingTileEntity = (HuoDingTileEntity) worldIn.getTileEntity(pos);
        if(huoDingTileEntity.isMaking()) {
            double d0 = (double) pos.getX() + Math.random() * 3 - 1.5;
            double d1 = (double) pos.getY() + Math.random() * 3 - 1.5;
            double d2 = (double) pos.getZ() + Math.random() * 3 - 1.5;
            if (rand.nextDouble() < 0.1D) {
                worldIn.playSound(d0, d1, d2, SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, SoundCategory.BLOCKS, 1.0F, 1.0F, false);
            }
            worldIn.addParticle(ParticleTypes.SMOKE, d0, d1, d2, 0.0D, 0.0D, 0.0D);
            worldIn.addParticle(ParticleTypes.FLAME, d0, d1, d2, 0.0D, 0.0D, 0.0D);
        }
    }
}
